package juc.cas;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicReference;

/*
* 原子引用 atomic reference
* */
public class Test7 {
    public static void main(String[] args) {
        AtomicReference<BigDecimal>atomicReference=new AtomicReference<>(new BigDecimal("10"));
        BigDecimal pre = atomicReference.get();
        BigDecimal next = pre.add(new BigDecimal("100"));
        atomicReference.compareAndSet(pre,next);
        System.out.println(atomicReference.get());
        AtomicReference<User> userAtomicReference = new AtomicReference<>(new User("zs",18));
        System.out.println(userAtomicReference.get());
        userAtomicReference.compareAndSet(userAtomicReference.get(),new User("ls",19));
        System.out.println(userAtomicReference.get());

    }

}
class User{
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    String name;
    int age;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
